<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <cpn></cpn>
        <cpn></cpn>
        <cpn></cpn>
    </div>

    <template id="cpn">
        <div>
            <h2>当前计数: {{counter}}</h2>
            <button @click="incr">+</button>
            <button @click="decr">-</button>
        </div>
    </template>

    <script src="../js/vue.js"></script>
    <script>
        Vue.component('cpn',{
            template: '#cpn',
            //通过函数返回对象的形式,保证每次创建组件实例对象时都有自己独立的对象作用域,使相互之间的作用域不会相互影响
            data(){
                return {
                    counter: 0
                }
            },
            methods: {
                incr(){
                    this.counter++
                },
                decr(){
                    this.counter--
                }
            }
        })

        const app = new Vue({
            el: '#app',
            data: {
                message: '苏州大学'
            }
        })
    </script>
</body>
</html>